using System.Collections.Generic;
using System.Linq;
using ExitGames.Client.Photon;
using UnityEngine;

public class BCWFlagManager : MonoBehaviour, IPunObservable
{
	public static BCWFlagManager instance;

	public float radius;

	private Vector2 centerPoint;

	public Dictionary<int, float> playersInRadiusScore = new Dictionary<int, float>();

	public int maxScore = 100;

	public int redTeamScore;

	public int blueTeamScore;

	public bool beganRestart;

	public bool twoTeamsInRadius;

	public PunTeams.Team winerTeam;

	public Renderer flagRenderer;

	private PhotonView photonView;

	private int lastRedResult;

	private int lastBlueResult;

	private int countInt = 8;

	private void Awake()
	{
		instance = this;
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void Start()
	{
		if (GameController.instance.curTypeGame != TypeGame.tanks && GameController.instance.curTypeGame != TypeGame.TDM && GameController.instance.curTypeGame != TypeGame.SkyBlock)
		{
			instance = null;
			Object.Destroy(base.gameObject);
		}
		base.gameObject.AddComponent<NJGMapItem>().type = 27;
		photonView = GetComponent<PhotonView>();
		centerPoint.x = base.transform.position.x;
		centerPoint.y = base.transform.position.z;
	}

	private void Update()
	{
		if (PVPManager.instance != null && PVPManager.instance.gameIsStarted)
		{
			if (PhotonNetwork.isMasterClient && GameController.instance != null && GameController.instance.playerScript != null)
			{
				foreach (PlayerBehavior listPlayer in GameController.instance.listPlayers)
				{
					if (CheckPlayerInRadius(listPlayer.transform.position) && !listPlayer.isDead)
					{
						if (!playersInRadiusScore.ContainsKey(listPlayer.photonView.ownerId))
						{
							playersInRadiusScore.Add(listPlayer.photonView.ownerId, 0f);
						}
					}
					else if (playersInRadiusScore.ContainsKey(listPlayer.photonView.ownerId))
					{
						playersInRadiusScore.Remove(listPlayer.photonView.ownerId);
					}
				}
				List<int> list = new List<int>(playersInRadiusScore.Keys);
				foreach (int item in list)
				{
					if (PhotonPlayer.Find(item) == null)
					{
						playersInRadiusScore.Remove(item);
					}
				}
				list = new List<int>(playersInRadiusScore.Keys);
				int redTeamPlayersCount = 0;
				int blueTeamPlayersCount = 0;
				list.ForEach(delegate(int p)
				{
					if (GameController.instance.FindPlayer(p).localTeam == PunTeams.Team.red)
					{
						redTeamPlayersCount++;
					}
					else
					{
						blueTeamPlayersCount++;
					}
				});
				if (redTeamPlayersCount == 0 || blueTeamPlayersCount == 0)
				{
					twoTeamsInRadius = false;
					foreach (int item2 in list)
					{
						float num = 0f;
						switch (list.Count)
						{
						case 1:
							num = 1f;
							break;
						case 2:
							num = 0.75f;
							break;
						default:
							num = 2f / (float)list.Count;
							break;
						}
						Dictionary<int, float> dictionary;
						Dictionary<int, float> dictionary2 = (dictionary = playersInRadiusScore);
						int key;
						int key2 = (key = item2);
						float num2 = dictionary[key];
						dictionary2[key2] = num2 + Time.deltaTime * num;
					}
				}
				else
				{
					twoTeamsInRadius = true;
				}
			}
			redTeamScore = 0;
			blueTeamScore = 0;
			foreach (KeyValuePair<int, float> item3 in playersInRadiusScore)
			{
				if (PhotonPlayer.Find(item3.Key) != null)
				{
					if (GameController.instance.FindPlayer(item3.Key).localTeam == PunTeams.Team.red)
					{
						redTeamScore += (int)item3.Value;
					}
					else
					{
						blueTeamScore += (int)item3.Value;
					}
				}
			}
			if (flagRenderer != null && flagRenderer.material != null)
			{
				if (twoTeamsInRadius || (redTeamScore == 0 && blueTeamScore == 0))
				{
					flagRenderer.material.color = Color.gray;
				}
				else if ((TDMController.myTeam == PunTeams.Team.red && instance.redTeamScore > 0) || (TDMController.myTeam == PunTeams.Team.blue && instance.blueTeamScore > 0))
				{
					flagRenderer.material.color = Color.blue;
				}
				else
				{
					flagRenderer.material.color = Color.red;
				}
			}
			if (PhotonNetwork.isMasterClient)
			{
				if (redTeamScore >= maxScore && lastRedResult < maxScore && lastBlueResult < maxScore)
				{
					photonView.RPC("EndRound", PhotonTargets.All, PunTeams.Team.red);
					BCWDebug.LogError("redTeam flag\n blueTeamScore: " + blueTeamScore + "\nredTeamScore: " + redTeamScore + "\nlastRedResult: " + lastRedResult + "\nlastBlueResult: " + lastBlueResult);
					lastRedResult = redTeamScore;
					lastBlueResult = blueTeamScore;
					return;
				}
				if (blueTeamScore >= maxScore && lastRedResult < maxScore && lastBlueResult < maxScore)
				{
					photonView.RPC("EndRound", PhotonTargets.All, PunTeams.Team.blue);
					BCWDebug.LogError("blueTeam flag\n blueTeamScore: " + blueTeamScore + "\nredTeamScore: " + redTeamScore + "\nlastRedResult: " + lastRedResult + "\nlastBlueResult: " + lastBlueResult);
				}
			}
		}
		lastRedResult = redTeamScore;
		lastBlueResult = blueTeamScore;
	}

	private bool CheckPlayerInRadius(Vector3 positionPlayer)
	{
		Vector2 a = default(Vector2);
		a.x = positionPlayer.x;
		a.y = positionPlayer.z;
		return Vector2.Distance(a, centerPoint) <= radius;
	}

	[PunRPC]
	public void EndRound(PunTeams.Team wt)
	{
		if (GameController.instance.curTypeGame == TypeGame.SkyBlock)
		{
			if (beganRestart)
			{
				return;
			}
			beganRestart = true;
		}
		winerTeam = wt;
		PVPManager.instance.TimeOut();
		if (TDMController.myTeam == wt && GameController.instance.curTypeGame == TypeGame.SkyBlock)
		{
			CupsManager.Cups += 3;
		}
		if (PhotonNetwork.isMasterClient)
		{
			ClearPlayers();
			Hashtable customProperties = PhotonNetwork.room.customProperties;
			if (customProperties.ContainsKey("StartRoundTime"))
			{
				customProperties["StartRoundTime"] = null;
			}
			else
			{
				customProperties.Add("StartRoundTime", null);
			}
			PhotonNetwork.room.SetCustomProperties(customProperties);
		}
	}

	public void ClearPlayers()
	{
		if (PhotonNetwork.isMasterClient)
		{
			playersInRadiusScore.Clear();
		}
	}

	public float GetPlayerScore(int playerId)
	{
		return (!playersInRadiusScore.ContainsKey(playerId)) ? (-1f) : ((float)Mathf.Clamp(Mathf.CeilToInt(playersInRadiusScore[playerId]), 1, maxScore));
	}

	public Dictionary<int, int> GetScores()
	{
		List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>();
		int num = 0;
		foreach (KeyValuePair<int, float> item in playersInRadiusScore)
		{
			int num2 = (int)GetPlayerScore(item.Key);
			list.Add(new KeyValuePair<int, int>(item.Key, num2));
			num += num2;
		}
		if (num == maxScore || list.Count == 0)
		{
			return list.ToDictionary((KeyValuePair<int, int> x) => x.Key, (KeyValuePair<int, int> x) => x.Value);
		}
		list.Sort((KeyValuePair<int, int> a, KeyValuePair<int, int> b) => a.Value.CompareTo(b.Value));
		int num3 = num - maxScore;
		int num4 = ((num3 < 0) ? 1 : (-1));
		int loops = Mathf.Abs(num3) / list.Count * num4;
		int takeFirst = Mathf.Abs(num3) % list.Count * num4;
		return list.Select((KeyValuePair<int, int> x, int ind) => new KeyValuePair<int, int>(x.Key, (ind >= takeFirst) ? (x.Value + loops) : (x.Value + loops + 1))).ToList().ToDictionary((KeyValuePair<int, int> x) => x.Key, (KeyValuePair<int, int> x) => x.Value);
	}

	public void UpdateIgnoredRound()
	{
	}

	public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		if (stream.isWriting)
		{
			List<int> list = new List<int>(playersInRadiusScore.Keys);
			stream.SendNext(playersInRadiusScore.Count);
			stream.SendNext(twoTeamsInRadius);
			{
				foreach (int item in list)
				{
					stream.SendNext(item);
					stream.SendNext((int)playersInRadiusScore[item]);
				}
				return;
			}
		}
		playersInRadiusScore.Clear();
		int num = (int)stream.ReceiveNext();
		twoTeamsInRadius = (bool)stream.ReceiveNext();
		for (int i = 0; i < num; i++)
		{
			int key = (int)stream.ReceiveNext();
			int num2 = (int)stream.ReceiveNext();
			float value = num2;
			playersInRadiusScore.Add(key, value);
		}
	}
}
